package com.javasm.maimaibao.login.service.impl;

import com.javasm.common.exception.JavasmException;
import com.javasm.common.exception.JavasmExceptionEnum;
import com.javasm.common.utils.RandomStringGenerator;
import com.javasm.common.utils.SmsUtil;
import com.javasm.maimaibao.login.entity.AdminUser;
import com.javasm.maimaibao.login.service.AdminUserService;
import com.javasm.maimaibao.login.service.LoginUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Administrator
 * @date 2025-02-12 16:06
 */
@Service
public class LoginUserServiceImpl implements LoginUserService {
    @Resource
    AdminUserService adminUserService;
    @Resource
    SmsUtil smsUtil;

//
//表单登录
    @Override
    public AdminUser doLogin(String username, String password) {
        AdminUser adminUser = adminUserService.getByName(username);
        if (adminUser==null){
            throw new JavasmException(JavasmExceptionEnum.NotLogin);
        }
        if (!adminUser.getPassword().equals(password)){
            throw new JavasmException(JavasmExceptionEnum.PasswordError);
        }
//        到这里密码正确，用户名存在，直接返回adminuser

        return adminUser;
    }
//手机号登录
    @Override
    public AdminUser phoneLogine(String code, String phone) {
//        保存手机号到adminuser
           AdminUser adminUser= adminUserService.getByPhone(phone);
           if (adminUser==null){
//               新用户,进行注册业务
               AdminUser adminUser1 = new AdminUser();
               adminUser1.setPhone(phone);

               adminUser1.setUsername("");
               adminUser1.setPassword("123456");
               Long time = new Date().getTime();
               adminUser1.setRegTime(time.toString());
//               adminUser1.setCreateUid(time.toString());
               adminUser1.setLoginTime(time.toString());
               adminUserService.save(adminUser1);
               throw new JavasmException(JavasmExceptionEnum.UserNotRegist);
           }
           //与用户输入比较
        String codeRandom = RandomStringGenerator.getCodeRandom();
        if (!codeRandom.equals(code)){
          throw new JavasmException(JavasmExceptionEnum.CodeError);
     }
     //标识验证码比对正确
//        提示登陆陈工跳转
        return adminUser;
    }

    @Override
    public AdminUser emailLogine(String code, String email) {
        //        保存邮箱到adminuser
        AdminUser adminUser= adminUserService.getByPhone(email);
        if (adminUser==null){
//               新用户,进行注册业务
            AdminUser adminUser1 = new AdminUser();
            adminUser1.setPhone(email);

//            adminUser1.setUsername("");
            adminUser1.setPassword("123456");
            Long time = new Date().getTime();
            adminUser1.setRegTime(time.toString());
//               adminUser1.setCreateUid(time.toString());
            adminUser1.setLoginTime(time.toString());
            adminUserService.save(adminUser1);
            throw new JavasmException(JavasmExceptionEnum.UserNotRegist);
        }


        return null;
    }

    @Override
    public void sendMsg(String phone) {
        String codeRandom = RandomStringGenerator.generateRandomNumberString(6);
        smsUtil.sendSms(phone,codeRandom);
    }


}
